package com.hrhx.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import com.github.duhongming.highcharts.json.GsonOption;
import com.github.duhongming.highcharts.series.Series;
import com.github.duhongming.highcharts.series.SeriesType;

public class HighChartsLineTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	private String title;
	private String subtitle;
	private String id;
	private String xCategoriesType;
	private String xAxisTitle;
	private String yAxisTitle;
	private List<String> xCategories;
	private Map<String,List<Object>> yCategories;
	private String url;
	@Override
	/**
		EVAL_BODY_INCLUDE：把Body读入存在的输出流中，doStartTag()函数可用
		EVAL_PAGE：继续处理页面，doEndTag()函数可用
		SKIP_BODY：忽略对Body的处理，doStartTag()和doAfterBody()函数可用
		SKIP_PAGE：忽略对余下页面的处理，doEndTag()函数可用
		EVAL_BODY_TAG：已经废止，由EVAL_BODY_BUFFERED取代
		EVAL_BODY_BUFFERED：申请缓冲区，由setBodyContent()函数得到的BodyContent对象来处理tag的body，
		如果类实现了BodyTag，那么doStartTag()可用，否则非法
	 */
	public int doStartTag() throws JspException {
		return BodyTag.EVAL_BODY_BUFFERED;//申请缓冲区
	}

	@SuppressWarnings("rawtypes")
	@Override
	public int doEndTag() throws JspException {
		//js开始位置
		String jsFuncStart="<script type=text/javascript>$(function () {$('#"+id+"').highcharts(";
		//js结束位置
		String jsFuncEnd=");});</script>";
		//得到标签体里面的内容
		//BodyContent bc= this.getBodyContent();
		//String content =bc.getString();
		
		//增强的Option
		GsonOption option = new GsonOption();

		//设置标题、设置子标题
		option.title(title,subtitle);
		
		//设置图表属性
		option.chart().margin(new int[]{75,75,95,75})
					  .type(SeriesType.line);
		
		//图表标签显示格式
		option.plotOptions().line().enableMouseTracking(true)
								   .cursor("pointer")
								   .dataLabels().enabled(true).format("{point.y:.2f}");
		
		//折线图链接配置
		option.plotOptions().series().point().events().click(
				"function () {" +
					" var para = 'Category=' + window.encodeURI(window.encodeURI(this.category)) + '&value=' + this.y + '&SeriesName=' + window.encodeURI(window.encodeURI(this.series.name)); " +
					" window.open('"+url+"'+para); "+
				"}");
		/**
		 * xAxis: {
            categories: Highcharts.getOptions().lang.shortMonths
        	},
		 */
		//设置x轴标题
		String[] categories_month={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
		String[] categories_week={"星期一", "星期二", "星期三", "星期三", "星期四", "星期五", "星期六","星期天"};
		option.xAxis().title().text(xAxisTitle);
		if(xCategoriesType.equalsIgnoreCase("months")){
			option.xAxis().categories(categories_month);
		}else if(xCategoriesType.equalsIgnoreCase("weekdays")){
			option.xAxis().categories(categories_week);
		}else{
			option.xAxis().categories(xCategories.toArray());
		}

		//设置y轴标题
		option.yAxis().title(yAxisTitle);
		
		//顶层map的记录数
		Set<Entry<String, List<Object>>>  set = yCategories.entrySet();
		Series[] series =new  Series[set.size()];
		//遍历顶层map，并得到list
		int i=0;
		for (String key :yCategories.keySet()) {
			//遍历list得到数据
			List<Object> list=yCategories.get(key);
			Double [] data= new Double [list.size()];
			int j=0;
			for(Object obj :list){
				if(obj!=null){
					data[j]=Double.parseDouble(obj.toString());
				}else{
					data[j]=null;
				}
				j++;
			}
			series[i] = new Series().data(data).name(key);
			i++;
		}
		option.series(series);
		try {
			this.pageContext.getOut().write(jsFuncStart+option.toString()+jsFuncEnd);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Tag.EVAL_PAGE;//继续处理页面
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSubtitle() {
		return subtitle;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getxCategoriesType() {
		return xCategoriesType;
	}

	public void setxCategoriesType(String xCategoriesType) {
		this.xCategoriesType = xCategoriesType;
	}

	public String getxAxisTitle() {
		return xAxisTitle;
	}

	public void setxAxisTitle(String xAxisTitle) {
		this.xAxisTitle = xAxisTitle;
	}

	public String getyAxisTitle() {
		return yAxisTitle;
	}

	public void setyAxisTitle(String yAxisTitle) {
		this.yAxisTitle = yAxisTitle;
	}

	public List<String> getxCategories() {
		return xCategories;
	}

	public void setxCategories(List<String> xCategories) {
		this.xCategories = xCategories;
	}

	public Map<String, List<Object>> getyCategories() {
		return yCategories;
	}

	public void setyCategories(Map<String, List<Object>> yCategories) {
		this.yCategories = yCategories;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
}
